/*
 * @lc app=leetcode.cn id=429 lang=cpp
 *
 * [429] N 叉树的层序遍历
 */

// @lc code=start
/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        if (root == nullptr)
            return {};

        vector<vector<int>> results;
        std::queue<Node*> nodes;
        nodes.push(root);

        while (!nodes.empty()) {
            int size = nodes.size();

            std::vector<int> result;
            result.reserve(size);
            for (int i = 0; i < size; i++) {
                auto node = nodes.front();
                nodes.pop();

                for (auto n : node->children)
                    nodes.push(n);

                result.push_back(node->val);
            }

            results.push_back(std::move(result));
        }

        return results;
    }

};
// @lc code=end

